/**
 * @version Create on 2012-11-2
 * @author Yinzi Chen
 */

public class MergeSortedArray {

	public void merge(int A[], int m, int B[], int n) {
		for (int i = m + n - 1; i >= n; --i) {
			A[i] = A[i - n];
		}
		int i = 0, j = n, k = 0;
		while (i < n && j < m + n) {
			if (A[j] > B[i]) {
				A[k++] = B[i++];
			} else {
				A[k++] = A[j++];
			}
		}
		while (i < n) {
			A[k++] = B[i++];
		}
	}

	public static void main(String[] args) {
		int[] a = new int[100];
		int[] b = new int[10];
		int m = 3, n = 5;
		a[0] = 1;
		a[1] = 5;
		a[2] = 8;
		b[0] = -1;
		b[1] = 2;
		b[2] = 2;
		b[3] = 4;
		b[4] = 6;
		MergeSortedArray mer = new MergeSortedArray();
		mer.merge(a, m, b, n);
		for (int i = 0; i < m + n; ++i)
			System.out.print(a[i] + " ");
	}

}
